package com.micromall.service;

import com.alibaba.fastjson.JSON;
import com.micromall.utils.ChainMap;
import com.micromall.utils.CommonEnvConstants;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * Created by zhangzx on 16/3/26.
 */
@Service
public class ShortMessageService {

	private static Logger logger = LoggerFactory.getLogger(ShortMessageService.class);

	private TaobaoClient taobaoClient;

	public static void main(String[] args) throws ApiException {
		TaobaoClient taobaoClient = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23393679", "0b2878cd626bb316df818b48daa6a1cb");

		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		// req.setExtend("123456");
		req.setSmsType("normal");
		req.setSmsFreeSignName("优农微商城");
		req.setSmsParamString("{\"code\":\"09890\"}");
		req.setRecNum("13023657670");
		req.setSmsTemplateCode("SMS_18290494");
		AlibabaAliqinFcSmsNumSendResponse rsp = taobaoClient.execute(req);
		System.out.println(rsp.getBody());
	}

	@PostConstruct
	public void init() {
		taobaoClient = new DefaultTaobaoClient(CommonEnvConstants.TAOBAO_SERVER_URL, CommonEnvConstants.TAOBAO_APP_KEY,
				CommonEnvConstants.TAOBAO_APP_SECRET);
	}

	/**
	 * 发送短信
	 *
	 * @param phone
	 * @param params
	 * @return
	 */
	private boolean sendMessage(String templateCode, String phone, Map<String, String> params) {
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		try {
			req.setSmsType("normal");
			req.setSmsFreeSignName("优农微商城");
			req.setSmsParamString(JSON.toJSONString(params));
			req.setRecNum(phone);
			req.setSmsTemplateCode(templateCode);
			AlibabaAliqinFcSmsNumSendResponse rsp = taobaoClient.execute(req);
			if (!rsp.isSuccess()) {
				logger.error("发送短信失败，短信请求：{}，返回结果：{}", JSON.toJSONString(req), rsp.getBody());
			}
			return rsp.isSuccess();
		} catch (Exception e) {
			logger.error("发送短信出错，短信请求：{}", JSON.toJSONString(req), e);
		}
		return false;
	}

	public boolean sendLoginVerifycode(String phone, String verifycode) {
		return sendMessage("SMS_18290494", phone, new ChainMap<>("code", verifycode));
	}

	public boolean sendWithdrawAuditNotify(String phone, String name, String stauts) {
		return sendMessage("SMS_11025197", phone, new ChainMap<>("name", name).append("stauts", stauts));
	}

	public boolean sendDeliveryNotify(String phone, String name, String orderid) {
		return sendMessage("SMS_10606464", phone, new ChainMap<>("name", name).append("orderid", orderid));
	}

	public boolean sendCertifiedAuditNotify(String phone, String name, String state) {
		return sendMessage("SMS_10661698", phone, new ChainMap<>("name", name).append("state", state));
	}
}
